﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace OutlookContactsSync.Services
{
    public static class Guard
    {
        public static void ArgumentNegative(int value, string argumentName)
        {
            if (!(value < 0))
            {
                throw new ArgumentException(string.Format("Argument '{0}' is {1}, but must be negative.", argumentName,
                                                          value));
            }
        }

        public static void ArgumentNonNegative(int value, string argumentName)
        {
            if (!(value >= 0))
            {
                throw new ArgumentException(string.Format("Argument '{0}' is {1}, but must be non-negative.",
                                                          argumentName, value));
            }
        }

        public static void ArgumentNotNull(object argumentValue, string argumentName)
        {
            if (argumentValue == null)
            {
                throw new ArgumentNullException(argumentName);
            }
        }

        public static void ArgumentPositive(int value, string argumentName)
        {
            if (!(value > 0))
            {
                throw new ArgumentException(string.Format("Argument '{0}' is {1}, but must be positive.", argumentName,
                                                          value));
            }
        }

        public static void ArrayNotNullOrEmpty(Array array, string argumentName)
        {
            if (array == null)
            {
                throw new ArgumentNullException(argumentName);
            }
            if (array.Length == 0)
            {
                throw new ArgumentException(string.Format("Argument '{0}' cannot be empty.", argumentName));
            }
        }

        public static void CollectionNotNullOrEmpty(ICollection collection, string argumentName)
        {
            if (collection == null)
            {
                throw new ArgumentNullException(argumentName);
            }
            if (collection.Count == 0)
            {
                throw new ArgumentException(string.Format("Argument '{0}' cannot be empty.", argumentName));
            }
        }

        public static void EnumerableNotNullOrEmpty<T>(IEnumerable<T> collection, string argumentName)
        {
            if (collection == null)
            {
                throw new ArgumentNullException(argumentName);
            }
            if (collection.Count() == 0)
            {
                throw new ArgumentException(string.Format("Argument '{0}' cannot be empty.", argumentName));
            }
        }

        public static void GuidNotEmpty(Guid argumentValue, string argumentName)
        {
            if (argumentValue.Equals(Guid.Empty))
            {
                throw new ArgumentException(string.Format("Argument '{0}' cannot be an empty Guid.", argumentName));
            }
        }

        public static void StringNotNullOrEmpty(string argumentValue, string argumentName)
        {
            if (string.IsNullOrEmpty(argumentValue))
            {
                throw new ArgumentException(string.Format("Argument '{0}' cannot be null or empty.", argumentName));
            }
        }
    }
}